/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Classifier.evaluation.DataResultProcess;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author kevin
 */
public class ProcessPanel extends JPanel {

    protected JButton m_OpenFileButton = new JButton("Select data");
    protected JLabel m_FilePath = new JLabel("the file path...");
    protected JList m_DataResultList = new JList();
    protected JTable m_DataResultTable = new JTable();
    protected JScrollPane m_ScrollPane;
    protected DefaultListModel m_ListMode = new DefaultListModel();
    protected JFileChooser m_FileChooser;
    DataResultProcess m_Process;
    protected ChartPanel m_ChartPanel;
    protected JPanel chartPanel =new JPanel();
    protected JPanel rightPanel = new JPanel();
    protected List m_DataList;
    private GraphicPanel gp;
    private JLabel aucLabel = new JLabel("The Area Under the Curve (AUC) is : ");

    public ProcessPanel() {
        initialize();
    }

    private void initialize() {
        m_OpenFileButton.setToolTipText("Open the data result that some classifier generated");
        m_OpenFileButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //读取数据
                setInstanceFromFile();
            }
        });

        this.setLayout(new BorderLayout());

        JPanel buttons = new JPanel();
        buttons.setSize(Container.WIDTH, 50);
        m_FilePath.setBorder(BorderFactory.createEtchedBorder());
        m_FilePath.setSize(200, 20);

        GroupLayout layout = new GroupLayout(buttons);
        buttons.setLayout(layout);
        buttons.setBorder(BorderFactory.createTitledBorder("Select file"));

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(
                layout.createSequentialGroup().addComponent(m_OpenFileButton).addComponent(m_FilePath, GroupLayout.PREFERRED_SIZE, 350, GroupLayout.PREFERRED_SIZE));
        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(m_OpenFileButton).addComponent(m_FilePath));

        this.add(buttons, BorderLayout.NORTH);
        JPanel dataPane = new JPanel();
        dataPane.setBorder(BorderFactory.createTitledBorder("Data view here"));
        dataPane.setSize(200, 700);
        m_DataResultTable = new JTable();
        m_DataResultTable.setPreferredScrollableViewportSize(new Dimension(200, 600));//设置表格的大小
        //m_DataResultTable.setRowHeight(30);//设置每行的高度为20
        //m_DataResultTable.setRowHeight(0, 20);//设置第1行的高度为15
        //m_DataResultTable.setRowMargin(5);//设置相邻两行单元格的距离
        m_DataResultTable.setRowSelectionAllowed(true);//设置可否被选择.默认为false
        m_DataResultTable.setSelectionBackground(Color.lightGray);//设置所选择行的背景色
        m_DataResultTable.setSelectionForeground(Color.blue);//设置所选择行的前景色
        m_DataResultTable.setGridColor(Color.black);//设置网格线的颜色
        m_DataResultTable.setShowGrid(false);//是否显示网格线
        m_DataResultTable.setShowHorizontalLines(false);//是否显示水平的网格线
        m_DataResultTable.setShowVerticalLines(true);//是否显示垂直的网格线
        m_DataResultTable.doLayout();
        m_DataResultTable.setBackground(Color.white);
        m_ScrollPane = new JScrollPane(m_DataResultTable);
        dataPane.add(m_ScrollPane);
        this.add(dataPane, BorderLayout.WEST);

       
        rightPanel.setSize(400, 400);
        rightPanel.setLayout(new BorderLayout());
        //creat chart panel
        gp = new GraphicPanel();
        double[] fpr = {0, 1};
        double[] tpr = {0, 1};
        gp.createDataset(fpr, tpr);
        JFreeChart freeChart = gp.createChart();
        m_ChartPanel = new ChartPanel(freeChart);
        chartPanel.add(m_ChartPanel);
        rightPanel.add(chartPanel,BorderLayout.CENTER);
        rightPanel.add(aucLabel,BorderLayout.SOUTH);
        /////
        this.add(rightPanel, BorderLayout.EAST);

        this.setVisible(true);

    }

    private void setInstanceFromFile() {
        m_FileChooser = new JFileChooser();
        m_FileChooser.addChoosableFileFilter(new CsvFliter());
        m_FileChooser.showOpenDialog(this);

        File file = m_FileChooser.getSelectedFile();
        m_Process = new DataResultProcess(file.getPath());
        m_DataList = m_Process.getDataResult();

        showDataResult();
        updateChart();
    }

    private void showDataResult() {
        int n = m_DataList.size();
        String[][] dataset = new String[n][3];
        String[] line = new String[2];
        for (int i = 0; i < n; i++) {
            line = (String[]) m_DataList.get(i);
            dataset[i][0] = Integer.toString(i + 1);
            dataset[i][1] = line[0];
            dataset[i][2] = line[1];
        }
        String[] columnNames = new String[3];
        columnNames[0] = "Number";
        columnNames[1] = "Class";
        columnNames[2] = "Probabilty";
        m_DataResultTable = new JTable(dataset, columnNames);
        m_FilePath.setText(m_Process.getFilePath());
        m_DataResultTable.validate();
        m_DataResultTable.repaint();
        m_ScrollPane.removeAll();
        m_ScrollPane = new JScrollPane(m_DataResultTable);
        this.add(m_ScrollPane);


    }
    private void updateChart(){
        List points = m_Process.getCurvePoints();
        int size = points.size();
        double[] line = new double[2];
        double[] tpr = new double[size];
        double[] fpr = new double[size];
        for(int i=0;i<size;i++){
            line = (double[])points.get(i);
            fpr[i]=line[0];
            tpr[i]=line[1];
        }
        gp.createDataset(fpr, tpr);
        JFreeChart freeChart = gp.createChart();
         m_ChartPanel = new ChartPanel(freeChart);
         if(m_Process.isDataSoft()){
            aucLabel.setText("The Area Under the Curve (AUC) is : "+m_Process.getAUC());
         }else{
             aucLabel.setText("The Area Under the Curve (AUC) is:"+m_Process.getAUC()+"             "
                     +"The Accuracy is : "+m_Process.getAccuracy());
         }
        chartPanel.removeAll();
        chartPanel.add(m_ChartPanel);
        chartPanel.validate();
        chartPanel.repaint();

    }

    ///过滤掉除csv后缀名的文件///

    class CsvFliter extends FileFilter {

        @Override
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(".csv") || f.isDirectory();
        }

        @Override
        public String getDescription() {
            return "csv file(.csv)";
        }
    }
    public String getTabTitle(){
        return "Process";
    }
    public String getTabTitleToolTip(){
        return "To process data result with ROC curve and AUC";
    }

    public static void main(String[] args) {

        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            final JFrame jf = new JFrame("Preprocess");
            ProcessPanel panel = new ProcessPanel();
            jf.getContentPane().setLayout(new BorderLayout());
            jf.getContentPane().add(panel, BorderLayout.CENTER);

            //jf.getContentPane()
            jf.addWindowListener(new WindowAdapter() {

                @Override
                public void windowClosing(WindowEvent e) {
                    jf.dispose();
                    System.exit(0);
                }
            });
            jf.pack();
            jf.setSize(1000, 700);
            jf.setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

